package day04;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Clientmm2 {
	String name;
	Socket soc;
	PrintWriter pw;
	BufferedReader br;
	public Clientmm2(String ip,int port,String name) throws UnknownHostException, IOException {
		soc=new Socket(ip,port);
		this.name=name;
		init();
		sendMessage(name);
	}
    public void init() throws IOException {
    	pw=new PrintWriter(soc.getOutputStream(),true);
    	br =new BufferedReader(new InputStreamReader(soc.getInputStream()));
    }
    public void sendMessage(String dd){
    		pw.println(dd);
    	
    }
    public void recieve() throws IOException {
    	Thread t=new Thread(new Runnable() {

			@Override
			public void run() {
				while(true) {
					try {
						System.out.println(br.readLine());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
			}
    	});
    	t.start();
    	
    }

	public static void main(String[] args) throws IOException, IOException {
			System.out.println("----这里是客户端----");
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入你的用户名");
		Clientmm2 c2=new Clientmm2("localhost",9800,sc.nextLine());
		c2.recieve();
		System.out.println("请输入你需要说的话");
		while(true) {			
			c2.sendMessage(sc.nextLine());
		}
	
		
		
		
	
  }
}
